//导入库
use rand::Rng;
use std::cmp::Ordering;
use std::io;

#[derive(Debug)]
pub struct Guess {
    value: i32,
}

impl Guess {
    pub fn new(value: i32) -> Guess {
        if value < 1 || value > 100 {
            panic!("Guess value must be between 1 and 100, got {}.", value);
        }

        Guess { value }
    }

    pub fn value(&self) -> i32 {
        self.value
    }
}

fn main() {
    println!("猜数");

    let secret_number = rand::thread_rng().gen_range(1, 101);
    println!("神秘数字是：{}", secret_number);

    loop {
        println!("猜测一个数：");

        //mut，声明可变的变量
        let mut guess: String = String::new();

        io::stdin().read_line(&mut guess).expect("msg:无法读取行");

        let guess: i32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => continue,
        };

        let guess = Guess::new(guess);

        println!("你猜测数的结果是：{:#?}", guess.value());

        // match guess.cmp(&secret_number) {
        //     Ordering::Less => println!("Too small"),
        //     Ordering::Greater => println!("Too big"),
        //     Ordering::Equal => {
        //         println!("you win");
        //         break;
        //     }
        // }
    }
}
